package com.swordget.service.impl;

import com.swordget.dao.CheckLogDao;
import com.swordget.dao.DataHandlerDao;
import com.swordget.entity.po.DataQueryPo;
import com.swordget.entity.vo.CheckLogVo;
import com.swordget.entity.vo.SimplePageResponseVo;
import com.swordget.service.CheckLogService;
import com.swordget.service.TableDefinitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: David
 * @Date: Created in 13:32 2020/5/7
 * @Description:
 * @Modified By: 审核相关service
 * @Version:
 */

@Service
public class CheckLogServiceImp implements CheckLogService {

    private DataHandlerDao dataHandlerDao;

    private CheckLogDao checkLogDao;

    private TableDefinitionService tableDefinitionService;

    @Autowired
    public CheckLogServiceImp(DataHandlerDao dataHandlerDao, CheckLogDao checkLogDao, TableDefinitionService tableDefinitionService) {
        this.dataHandlerDao = dataHandlerDao;
        this.checkLogDao = checkLogDao;
        this.tableDefinitionService = tableDefinitionService;
    }


    final static List<String> COLUMNS = Arrays.asList(new String[]{"SERIALNO", "TABLEID", "TABLECOMMENT", "CREATEUSER",
            "MAKEUSER", "MAKESTATUS", "CREATEDATE", "CREATETIME", "MAKEDATE", "MAKETIME"});

    /**
     * 分页查询数据
     * 有轮子 我不想在写 DAO层了,复用轮子就行了
     * @param page
     * @param rows
     * @return
     */
    @Override
    public SimplePageResponseVo findAllByPage(int page, int rows) {

        String[] typeArray = new String[10];
        Arrays.fill(typeArray, "VARCHAR");
        List<String> types = Arrays.asList(typeArray);
        DataQueryPo dataQueryPo = dataHandlerDao.query(rows, page, null, "CHECKLOG",
                COLUMNS, types, null, "");

        List<Map<String, Object>> maps = dataQueryPo.getData();

        List<CheckLogVo> checkLogVos = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            checkLogVos.add(CheckLogVo.builder()
                    .serialNo((String) map.get("SERIALNO"))
                    .tableId((String) map.get("TABLEID"))
                    .tableComment((String) map.get("TABLECOMMENT"))
                    .makeUser((String) map.get("MAKEUSER"))
                    .makeDate((String) map.get("MAKEDATE"))
                    .makeTime((String) map.get("MAKETIME"))
                    .makeStatus((String) map.get("MAKESTATUS"))
                    .createDate((String) map.get("CREATEDATE"))
                    .createTime((String) map.get("CREATETIME"))
                    .createUser((String) map.get("CREATEUSER"))
                    .build());
        }
        return SimplePageResponseVo.builder()
                .rows(checkLogVos)
                .total(dataQueryPo.getTotal())
                .build();
    }

    @Override
    public boolean modify(CheckLogVo checkLogVos, String loginId) {

        final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        checkLogVos.setMakeUser(loginId);
        checkLogVos.setMakeDate(dateFormat.format(new Date()));
        checkLogVos.setMakeTime(timeFormat.format(new Date()));
        //获取表名
        String tableName = tableDefinitionService.getTableName(loginId, checkLogVos.getTableId());
        //获取所有字段
        List<String> columns = tableDefinitionService.getColumns(checkLogVos.getTableId(), true, false);
        return checkLogDao.modify(checkLogVos, tableName, columns);

    }


}
